package com.heima.wemedia.service.impl;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.BeanUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmChannelPageDto;
import com.heima.model.wemedia.pojos.WmChannel;
import com.heima.model.wemedia.pojos.WmNews;
import com.heima.wemedia.mapper.WmChannelMapper;
import com.heima.wemedia.mapper.WmNewsMapper;
import com.heima.wemedia.service.WmChannelService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
@Slf4j
@RequiredArgsConstructor
public class WmChannelServiceImpl extends ServiceImpl<WmChannelMapper, WmChannel> implements WmChannelService {


    //channel
    private final WmChannelMapper wmChannelMapper;

    //wmnews
    private final WmNewsMapper wmNewsMapper;
    /**
     * 查询所有频道
     *
     * @return
     */
    @Override
    public ResponseResult findAll() {
        return ResponseResult.okResult(list());
    }

    /**
     * 增加频道
     *
     * @param wmChannel
     * @return
     */
    @Override
    public ResponseResult saveChannel(WmChannel wmChannel) {

        //1.检查参数
        if (wmChannel ==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"管理端保存频道参数不存在");
        }


        //2.检查改频道是否存在
        List<WmChannel> wmChannels = wmChannelMapper.selectList(
                Wrappers.<WmChannel>lambdaQuery().eq(WmChannel::getName, wmChannel.getName())
        );

        //返回频道存在信息
        if (wmChannels.size() > 0){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"该频道已存在");
        }

        //设置参数
        wmChannel.setIsDefault(true); //设置为默认
        wmChannel.setCreatedTime(new Date());

        //3.不存在 新增频道
        int insert = wmChannelMapper.insert(wmChannel);
        if (insert <= 0){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"频道新增失败");
        }


        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }



    /**
     * 管理端 分页查询频道
     *
     * @param wmChannelPageDto
     * @return
     */
    @Override
    public ResponseResult listChannel(WmChannelPageDto wmChannelPageDto) {

        //1.检查参数
        //分页检查
        wmChannelPageDto.checkParam();

        //2.分页查询
        IPage page =new Page(wmChannelPageDto.getPage(),wmChannelPageDto.getSize());
        LambdaQueryWrapper<WmChannel> lambdaQueryWrapper = new LambdaQueryWrapper();

        //3.查询需要按照创建时间倒序查询
        if (wmChannelPageDto.getCreatedTime()!=null){
           lambdaQueryWrapper.orderByDesc(WmChannel::getCreatedTime);
        }

        //4.按照频道名称模糊查询
        if (wmChannelPageDto.getName()!=null){
            lambdaQueryWrapper.like(WmChannel::getName,wmChannelPageDto.getName());
        }

        //5.可以按照状态进行精确查找（1：启用   true           0：禁用   false）
        if (wmChannelPageDto.getStatus()!=null){
            lambdaQueryWrapper.eq(WmChannel::getStatus,wmChannelPageDto.getStatus());
        }

        //6.分页查询
        page=page(page,lambdaQueryWrapper);

        //3.结果返回
        ResponseResult responseResult = new PageResponseResult(wmChannelPageDto.getPage(), wmChannelPageDto.getSize(), (int) page.getTotal());
        responseResult.setData(page.getRecords());


        return responseResult;
    }



    /**
     * 管理端 修改频道
     *
     * @param wmChannel
     * @return
     */
    @Override
    public ResponseResult updateChannel(WmChannel wmChannel) {

        //1.判断参数是否有效
        if (wmChannel==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //2.判断当前频道是否被引用
        List<WmNews> wmNews = wmNewsMapper.selectList(Wrappers.<WmNews>lambdaQuery().eq(WmNews::getChannelId, wmChannel.getId()));
        if (wmNews.size() > 0){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"该频道已被引用");
        }


        //3.属性拷贝
        WmChannel wmCha=new WmChannel();
        BeanUtil.copyProperties(wmChannel,wmCha);

        //4.设置修改时间
        wmCha.setCreatedTime(new Date());
        wmCha.setIsDefault(true);

        //5.修改
        wmChannelMapper.updateById(wmCha);

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }


    /**
     * 管理端 删除频道
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult deleteChannel(Integer id) {


        //1.判断参数是否有效
        if (id==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //2.判断当前频道是否被引用
        List<WmNews> wmNews = wmNewsMapper.selectList(Wrappers.<WmNews>lambdaQuery().eq(WmNews::getChannelId, id));
        if (wmNews.size() > 0){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"该频道已被引用");
        }


        //3.修改
        wmChannelMapper.deleteById(id);

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);

    }
}













